package com.turbo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRabbit
public class RabbitConfig {

    /**
     * 正常队列，设置过期时间，关联死信交换器，
     * @return
     */
    @Bean
    public Queue queue(){

        Map<String,Object> argument = new HashMap<>();
        // 消息生存时间 10s
        argument.put("x-message-ttl",10*1000);
        // 该队列关联的死信交换器（当队列消息TTL到期后依然没有消费，则加入死信队列）
        argument.put("x-dead-letter-exchange","ex.dlx");
        // 指定死信交换器的路由键
        argument.put("x-dead-letter-routing-key","key.dlx");

        return new Queue("queue.order",true,false,false,argument);
    }

    /**
     * 声明正常的交换器
     * @return
     */
    @Bean
    public Exchange exchange(){
        return new DirectExchange("ex.order",
                true,
                false,
                null);
    }

    /**
     * 绑定
     */
    @Bean
    public Binding binding(){

        return new Binding("queue.order",
                Binding.DestinationType.QUEUE,
                "ex.order",
                "key.order",
                null);
    }

    /**
     * 声明死信队列
     */
    @Bean
    public Queue queueDLX(){
        return new Queue("queue.dlx",
                true,
                false,
                false,
                null);
    }

    /**
     * 声明死信交换器
     */
    @Bean
    public Exchange exchangeDLX(){
        return new DirectExchange("ex.dlx",
                true,
                false,
                null);
    }

    /**
     * 绑定
     */
    @Bean
    public Binding bindingDLX(){
        return new Binding("queue.dlx",
                Binding.DestinationType.QUEUE,
                "ex.dlx",
                "key.dlx",
                null);
    }

}
